Skilled in SEO, content writing, and digital marketing. Completed several years of working in many organizations including multinational companies. I love to learn new things in life that keep me motivated.
Sure, you can use the GROUP_CONCAT() function to return all items in a single row in MySQL. The syntax is as follows:
SQL
SELECT GROUP_CONCAT(column_name) AS column_name
FROM table_name;
For example, if you have a table called products with the columns
name and price, you could use the following query to return all product names in a single row:
SQL
SELECT GROUP_CONCAT(name) AS names
FROM products;
This would return a single row with a comma-separated list of all product names.
Here is an example of how to use the GROUP_CONCAT() function in MySQL:
SQL
CREATE TABLE products (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
price INT NOT NULL,
PRIMARY KEY (id)
);
INSERT INTO products (name, price) VALUES
('Product 1', 100),
('Product 2', 200),
('Product 3', 300);
SELECT GROUP_CONCAT(name) AS names
FROM products;
This will return the following result:
names
Product 1,Product 2,Product 3
As you can see, the GROUP_CONCAT() function has returned all product names in a single row, separated by commas.
Liked By
Write Answer
MySQL - Return all items in one row.
Join MindStick Community
You have need login or register for voting of answers or question.
Aryan Kumar
28-Jul-2023Sure, you can use the
GROUP_CONCAT()
function to return all items in a single row in MySQL. The syntax is as follows:SQL
For example, if you have a table called
products
with the columnsname
andprice
, you could use the following query to return all product names in a single row:SQL
This would return a single row with a comma-separated list of all product names.
Here is an example of how to use the
GROUP_CONCAT()
function in MySQL:SQL
This will return the following result:
As you can see, the
GROUP_CONCAT()
function has returned all product names in a single row, separated by commas.